// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/dwio/dwrf/writer/physical_size_aggregator.h>

using namespace ::testing;

namespace kumo::pollux::dwrf {
TEST(PhysicalSizeAggregatorTest, UpdateLeaf) {
  auto leafOne = std::make_unique<PhysicalSizeAggregator>(nullptr);
  auto leafTwo = std::make_unique<PhysicalSizeAggregator>(nullptr);
  ASSERT_EQ(0, leafOne->getResult());
  ASSERT_EQ(0, leafTwo->getResult());

  leafOne->recordSize(
      DwrfStreamIdentifier{0, 0, 0, StreamKind::StreamKind_DATA}, 1);
  EXPECT_EQ(1, leafOne->getResult());
  EXPECT_EQ(0, leafTwo->getResult());

  leafTwo->recordSize(
      DwrfStreamIdentifier{1, 0, 0, StreamKind::StreamKind_DATA}, 2);
  EXPECT_EQ(1, leafOne->getResult());
  EXPECT_EQ(2, leafTwo->getResult());

  leafOne->recordSize(
      DwrfStreamIdentifier{2, 0, 0, StreamKind::StreamKind_DATA}, 4);
  EXPECT_EQ(5, leafOne->getResult());
  EXPECT_EQ(2, leafTwo->getResult());
}

TEST(PhysicalSizeAggregatorTest, UpdateParent) {
  auto parent = std::make_unique<PhysicalSizeAggregator>(nullptr);
  auto childOne = std::make_unique<PhysicalSizeAggregator>(parent.get());
  auto childTwo = std::make_unique<PhysicalSizeAggregator>(parent.get());
  ASSERT_EQ(0, parent->getResult());
  ASSERT_EQ(0, childOne->getResult());
  ASSERT_EQ(0, childTwo->getResult());

  parent->recordSize(
      DwrfStreamIdentifier{0, 0, 0, StreamKind::StreamKind_DATA}, 1);
  ASSERT_EQ(1, parent->getResult());
  ASSERT_EQ(0, childOne->getResult());
  ASSERT_EQ(0, childTwo->getResult());

  childOne->recordSize(
      DwrfStreamIdentifier{1, 0, 0, StreamKind::StreamKind_DATA}, 2);
  EXPECT_EQ(3, parent->getResult());
  EXPECT_EQ(2, childOne->getResult());
  EXPECT_EQ(0, childTwo->getResult());

  childTwo->recordSize(
      DwrfStreamIdentifier{2, 0, 0, StreamKind::StreamKind_DATA}, 4);
  EXPECT_EQ(7, parent->getResult());
  EXPECT_EQ(2, childOne->getResult());
  EXPECT_EQ(4, childTwo->getResult());
}
} // namespace kumo::pollux::dwrf
